using System;
using System.Collections.Generic;
using System.Text;

namespace Pampa.Common.Utils
{
    public class MultiSet<T>
    {
        private SortedList<T, int> list;

        public MultiSet()
        {
            list = new SortedList<T, int>();
        }
        
        public int Add(T item)
        {
            if (list.ContainsKey(item))
                list[item]++;
            else
                list.Add(item, 1);

            return list[item];
        }

        public void Remove(T item)
        { 
            if(list.ContainsKey(item))
            {
                if(list[item]==1)
                    list.Remove(item);
                else
                    list[item]--;
            }
        }

        public void RemoveAll(T item)
        {
            list.Remove(item);
        }

        public bool Contains(T item)
        {
            return list.ContainsKey(item);
        }

        public int Count(T item)
        {
            if (list.ContainsKey(item))
                return list[item];
            else
                return 0;
        }

        public void Clear()
        { 
            list.Clear();
        }

    }
}
